package com.xy.bizportdemo.setting;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.xy.bizportdemo.PrivateActivity;
import com.xy.bizportdemo.R;
import com.xy.bizportdemo.database.SmsDBHelper;
import com.xy.bizportdemo.task.LoadTaskManager;
import com.xy.bizportdemo.util.BizportContentUtil;
import com.xy.bizportdemo.util.ImportDataUtil;
import com.xy.bizportdemo.util.SettingStateUtil;

import static com.xy.bizportdemo.task.LoadBaseAsyncTask.STATUS_ERROR;
import static com.xy.bizportdemo.task.LoadBaseAsyncTask.STATUS_FINISH;
import static com.xy.bizportdemo.task.LoadBaseAsyncTask.STATUS_IDLE;
import static com.xy.bizportdemo.task.LoadBaseAsyncTask.STATUS_LOADED;
import static com.xy.bizportdemo.task.LoadBaseAsyncTask.STATUS_LOADING;

public class SettingActivity extends FragmentActivity {
    private static final String TAG = "SettingActivity";

    private static final int KEY_OPEN_FILE = 1;
    private static final int TYPE_SYSTEM = 1;
    private static final int TYPE_XML = 2;

    private SharedPreferences mSharedPreferences;
    private Editor mEditor;

    private RelativeLayout mStrictModeLayout;
    private Switch mStrictModeSwitch;
    private RelativeLayout mRichBubbleModeLayout;
    private Switch mRichBubbleModeSwitch;
    private RelativeLayout mRecgModeSwitchLayout;
    private Switch mRecgModeSwitch;
    private RelativeLayout mSensitiveInfoModeSwitchLayout;
    private Switch mSensitiveInfoModeSwitch;
    private RelativeLayout mSmsTraceModeSwitchLayout;
    private Switch mSmsTraceModeSwitch;
    private RelativeLayout mBaseParseModeSwitchLayout;
    private Switch mBaseParseModeSwitch;
    private RelativeLayout mPubInfoModeSwitchLayout;
    private Switch mPubInfoModeSwitch;
    private RelativeLayout mMenuModeSwitchLayout;
    private Switch mMenuModeSwitch;
    private RelativeLayout mDebugModeSwitchLayout;
    private Switch mDebugModeSwitch;
    private RelativeLayout mNoticeModeSwitchLayout;
    private Switch mNoticeModeSwitch;
    private RelativeLayout mClearModeSwitchLayout;
    private Switch mClearModeSwitch;
    private RelativeLayout mRichBubbleFullKeyModeLayout;
    private Switch mRichBubbleFullKeyModeSwitch;

    private RelativeLayout mInsertSms;
    private RelativeLayout mInsertSms_toApp;
    private RelativeLayout mDefaltSms;
    private RelativeLayout mClearBuffer;
    private RelativeLayout mToTestMod;
    private RelativeLayout mDuoquPrivate;
    private RelativeLayout mRlDuoquSdk;

    private LinearLayout mToolbar;

    private ProgressBar mInsertToAppProgress = null;
    private TextView mFinishToAppTextView = null;
    private ProgressBar mInsertProgress = null;
    private TextView mFinishTextView = null;
    private Context mContext = null;

    private LoadTaskManager mLoadTaskManager = null;
    private SettingSDKBean mSettingSDKBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: " + savedInstanceState);
        initTitleBar();

        mSharedPreferences = this.getSharedPreferences(BizportContentUtil.SETTING_FILE_NAME,
                Context.CONTEXT_IGNORE_SECURITY);
        mEditor = mSharedPreferences.edit();
        mContext = this;

        initView();
        initSettingElement();
        initSmsLoader();
    }

    private void updateLoaderStatus(int type, int status) {
        int progressView;
        int textView;
        switch (status) {
            case STATUS_LOADING:
                progressView = View.VISIBLE;
                textView = View.GONE;
                break;

            case STATUS_LOADED:
            case STATUS_FINISH:
                progressView = View.GONE;
                textView = View.VISIBLE;
                break;

            case STATUS_IDLE:
            case STATUS_ERROR:
            default:
                progressView = View.GONE;
                textView = View.GONE;
                break;
        }

        if (type == TYPE_SYSTEM) {
            mInsertProgress.setVisibility(progressView);
            mFinishTextView.setVisibility(textView);
            switchView(mInsertSms, !(progressView == View.VISIBLE));
        } else if (type == TYPE_XML) {
            mInsertToAppProgress.setVisibility(progressView);
            mFinishToAppTextView.setVisibility(textView);
            switchView(mInsertSms_toApp, !(progressView == View.VISIBLE));
        }

        if (status == STATUS_FINISH) {
            notifyImportFinish();
        }
    }

    private void deInitSmsLoader() {
        mLoadTaskManager.setOnSystemLoadListener(null);
        mLoadTaskManager.setOnPathLoadListener(null);
        mLoadTaskManager = null;
    }

    private void initSmsLoader() {
        mLoadTaskManager = LoadTaskManager.getInstance(mContext);
        mLoadTaskManager.setOnSystemLoadListener(new LoadTaskManager.OnLoadListener() {
            @Override
            public void onProgress(int current, int total) {

            }

            @Override
            public void onStatus(int status) {
                updateLoaderStatus(TYPE_SYSTEM, status);
            }
        });

        mLoadTaskManager.setOnPathLoadListener(new LoadTaskManager.OnLoadListener() {
            @Override
            public void onProgress(int current, int total) {

            }

            @Override
            public void onStatus(int status) {
                updateLoaderStatus(TYPE_XML, status);
            }
        });
    }

    @Override
    protected  void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected  void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    private void initTitleBar() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);

        mToolbar = (LinearLayout) findViewById(R.id.common_back);
        mToolbar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mInsertProgress = (ProgressBar) findViewById(R.id.progressbar_sms_loading);
        mFinishTextView = (TextView) findViewById(R.id.insert_loaded);
        mInsertToAppProgress = (ProgressBar) findViewById(R.id.progressbar_sms_toapp_loading);
        mFinishToAppTextView = (TextView) findViewById(R.id.insert_toapp_loaded);
        mInsertProgress.setVisibility(View.GONE);
        mFinishTextView.setVisibility(View.GONE);
        mInsertToAppProgress.setVisibility(View.GONE);
        mFinishToAppTextView.setVisibility(View.GONE);
    }

    private void initView() {
        //StrictMode
        mStrictModeLayout = (RelativeLayout) findViewById(R.id.view_strictmode);
        mStrictModeSwitch = (Switch) findViewById(R.id.switch_strichmode);
        mStrictModeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mStrictModeSwitch.setChecked(mStrictModeSwitch.isChecked() ? false : true);
            }
        });
        mStrictModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.STRICT_MODE, isChecked);
                mEditor.commit();
            }
        });
        // 丰富气泡
        mRichBubbleModeLayout = (RelativeLayout) findViewById(R.id.richbubble_mode_layout);
        mRichBubbleModeSwitch = (Switch) findViewById(R.id.richbubble_mode);
        mRichBubbleModeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mRichBubbleModeSwitch.setChecked(mRichBubbleModeSwitch.isChecked() ? false : true);
            }
        });
        mRichBubbleModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.RICHBUBBLE_MODE, isChecked);
                mEditor.commit();
            }
        });

        //全KEY展示
        mRichBubbleFullKeyModeLayout = (RelativeLayout) findViewById(R.id.richbubble_fullkey_mode_layout);
        mRichBubbleFullKeyModeSwitch = (Switch) findViewById(R.id.richbubble_fullkey_mode);
        mRichBubbleFullKeyModeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mRichBubbleFullKeyModeSwitch.isChecked()) {
                    mRichBubbleFullKeyModeSwitch.setChecked(false);
                } else {
                    mRichBubbleFullKeyModeSwitch.setChecked(true);
                }
            }
        });

        mRichBubbleFullKeyModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.FULLKEY_MODE, isChecked);
                mEditor.commit();
            }
        });

        if (SettingStateUtil.ALLKEY_MODE) {
            mRichBubbleFullKeyModeLayout.setVisibility(View.VISIBLE);
        } else {
            mRichBubbleFullKeyModeSwitch.setChecked(false);
            mRichBubbleFullKeyModeLayout.setVisibility(View.GONE);
        }

        // 特征值
        mRecgModeSwitchLayout = (RelativeLayout) findViewById(R.id.recg_mode_layout);
        mRecgModeSwitch = (Switch) findViewById(R.id.recg_mode);
        mRecgModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mRecgModeSwitch.isChecked()) {
                    mRecgModeSwitch.setChecked(false);
                } else {
                    mRecgModeSwitch.setChecked(true);
                }
            }
        });
        mRecgModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.EIGENVALUE_MODE, isChecked);
                mEditor.commit();
            }
        });

        // 敏感信息与网址校验
        mSensitiveInfoModeSwitchLayout = (RelativeLayout) findViewById(R.id.sensitive_mode_layout);
        mSensitiveInfoModeSwitch = (Switch) findViewById(R.id.sensitive_mode);
        mSensitiveInfoModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mSensitiveInfoModeSwitch.isChecked()) {
                    mSensitiveInfoModeSwitch.setChecked(false);
                } else {
                    mSensitiveInfoModeSwitch.setChecked(true);
                }
            }
        });
        mSensitiveInfoModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.SENSITIVEINFO_MODE, isChecked);
                mEditor.commit();
            }
        });

        // 短信来源鉴真
        mSmsTraceModeSwitchLayout = (RelativeLayout) findViewById(R.id.sms_trace_mode_layout);
        mSmsTraceModeSwitch = (Switch) findViewById(R.id.sms_trace_mode);
        mSmsTraceModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mSmsTraceModeSwitch.isChecked()) {
                    mSmsTraceModeSwitch.setChecked(false);
                } else {
                    mSmsTraceModeSwitch.setChecked(true);
                }
            }
        });
        mSmsTraceModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.SMS_TRACE_MODE, isChecked);
                mEditor.commit();
            }
        });
        if (SettingStateUtil.SMS_TRACE_MODE){
            mSmsTraceModeSwitchLayout.setVisibility(View.VISIBLE);
        }else {

            mSmsTraceModeSwitchLayout.setVisibility(View.GONE);
        }

        // 基础识别
        mBaseParseModeSwitchLayout = (RelativeLayout) findViewById(R.id.base_parse_mode_layout);
        mBaseParseModeSwitch = (Switch) findViewById(R.id.base_parse_mode);
        mBaseParseModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mBaseParseModeSwitch.isChecked()) {
                    mBaseParseModeSwitch.setChecked(false);
                } else {
                    mBaseParseModeSwitch.setChecked(true);
                }
            }
        });
        mBaseParseModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.BASE_PARSE_MODE, isChecked);
                mEditor.commit();
                
                if (mRichBubbleModeSwitch != null) {
                    if (!isChecked){
                        mRichBubbleModeSwitch.setChecked(false);
                        mRichBubbleModeSwitch.setClickable(false);
                        //MatchCacheManager.deleteAll();
                    }else{
                        mRichBubbleModeSwitch.setClickable(true);
                    }
                }
            }
        });

        // 企业资料
        mPubInfoModeSwitchLayout = (RelativeLayout) findViewById(R.id.pub_info_mode_layout);
        mPubInfoModeSwitch = (Switch) findViewById(R.id.pub_info_mode);
        mPubInfoModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mPubInfoModeSwitch.isChecked()) {
                    mPubInfoModeSwitch.setChecked(false);
                } else {
                    mPubInfoModeSwitch.setChecked(true);
                }
            }
        });
        mPubInfoModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                BizportContentUtil.publicInfo_mode = isChecked;
                mEditor.putBoolean(BizportContentUtil.PUB_INFO_MODE, isChecked);
                mEditor.commit();
            }
        });

        if (SettingStateUtil.XY_PUBINFO_MODE){
            mPubInfoModeSwitchLayout.setVisibility(View.VISIBLE);
        } else {
            mPubInfoModeSwitchLayout.setVisibility(View.GONE);
        }

        // 菜单
        mMenuModeSwitchLayout = (RelativeLayout) findViewById(R.id.menu_mode_layout);
        mMenuModeSwitch = (Switch) findViewById(R.id.menu_mode);
        mMenuModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mMenuModeSwitch.isChecked()) {
                    mMenuModeSwitch.setChecked(false);
                } else {
                    mMenuModeSwitch.setChecked(true);
                }
            }
        });
        mMenuModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.MENU_MODE, isChecked);
                mEditor.commit();
            }
        });

        // debug
        mDebugModeSwitchLayout = (RelativeLayout) findViewById(R.id.debug_mode_layout);
        mDebugModeSwitch = (Switch) findViewById(R.id.debug_mode);
        mDebugModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mDebugModeSwitch.isChecked()) {
                    mDebugModeSwitch.setChecked(false);
                } else {
                    mDebugModeSwitch.setChecked(true);
                }
            }
        });
        mDebugModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.DEBUG_MODE, isChecked);
                mEditor.commit();
            }
        });

        // 通知栏
        mNoticeModeSwitchLayout = (RelativeLayout) findViewById(R.id.notice_mode_layout);
        mNoticeModeSwitch = (Switch) findViewById(R.id.notice_mode);
        mNoticeModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mNoticeModeSwitch.isChecked()) {
                    mNoticeModeSwitch.setChecked(false);
                } else {
                    mNoticeModeSwitch.setChecked(true);
                }
            }
        });
        mNoticeModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.NOTICE_MODE, isChecked);
                mEditor.commit();
            }
        });

        // 插入短信
        mInsertSms = (RelativeLayout) findViewById(R.id.insert_sms_layout);
        mInsertSms.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mLoadTaskManager.executeSystem();
            }
        });

        //插入短信到手机数据库
        mInsertSms_toApp = (RelativeLayout) findViewById(R.id.insert_sms_toapp_layout);
        mInsertSms_toApp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("text/plain");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                try {
                    startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), KEY_OPEN_FILE);
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(SettingActivity.this, "需要安装文件浏览器", Toast.LENGTH_SHORT).show();
                }

            }
        });

        //清除数据库
        View viewClearSms = findViewById(R.id.duoqu_clear_sms_layout);
        viewClearSms.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ContentResolver cr = mContext.getContentResolver();
                cr.delete(SmsDBHelper.URI_MESSAGE, null, null);
                Toast.makeText(SettingActivity.this, "短信清除成功", Toast.LENGTH_SHORT).show();
            }
        });

        // 申请默认短信应用权限
        mDefaltSms = (RelativeLayout) findViewById(R.id.setdefaltsms_layout);
        mDefaltSms.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ImportDataUtil.changeToDefaultSms(mContext);
            }
        });
        
        //清除缓存
        mClearBuffer = (RelativeLayout) findViewById(R.id.clear_buffer_layout);
        mClearBuffer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //MatchCacheManager.deleteAll();
                Toast.makeText(SettingActivity.this, "清除缓存", Toast.LENGTH_LONG).show();
            }
        });

        //测试模式
        mToTestMod = (RelativeLayout) findViewById(R.id.test_mod_layout);
//        mToTestMod.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                Intent intent = new Intent();
//                intent.setClass(SettingActivity.this, TestModeFirstActivity.class);
//                startActivity(intent);
//
//            }
//        });
        if(SettingStateUtil.TEST_MODE){
            mToTestMod.setVisibility(View.VISIBLE);
        }else{
            mToTestMod.setVisibility(View.GONE);
        }

        //隐私声明
        mDuoquPrivate = (RelativeLayout) findViewById(R.id.duoqu_private_layout);
        mDuoquPrivate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this, PrivateActivity.class);
                startActivity(intent);

            }
        });

        //设置小源SDK
        mRlDuoquSdk = (RelativeLayout) findViewById(R.id.duoqu_rl_sdk);
        mRlDuoquSdk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                showSettingSDKDialog();
            }
        });

        //清除丰富气泡内存缓存
        mClearModeSwitchLayout = (RelativeLayout) findViewById(R.id.clear_mode_layout);
        mClearModeSwitch = (Switch) findViewById(R.id.clear_mode);
        mClearModeSwitchLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mClearModeSwitch.isChecked()) {
                    mClearModeSwitch.setChecked(false);
                } else {
                    mClearModeSwitch.setChecked(true);
                }
            }
        });
        mClearModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEditor.putBoolean(BizportContentUtil.CLEAR_MODE, isChecked);
                mEditor.commit();
            }
        });
    }

    private void switchView(View v, boolean on) {
        Log.i(TAG, "switchView: view=" + v + " enable:" + on);
        v.setClickable(on);
        v.setEnabled(on);
    }

    private void initSettingElement() {
        mStrictModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.STRICT_MODE, false));
        mRichBubbleModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.RICHBUBBLE_MODE, true));
        mRecgModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.EIGENVALUE_MODE, true));
        mSensitiveInfoModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.SENSITIVEINFO_MODE, true));
        mSmsTraceModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.SMS_TRACE_MODE, true));
        mBaseParseModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.BASE_PARSE_MODE, true));
        mPubInfoModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.PUB_INFO_MODE, true));
        mMenuModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.MENU_MODE, true));
        mDebugModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.DEBUG_MODE, false));
        mNoticeModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.NOTICE_MODE, false));
        mClearModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.CLEAR_MODE, true));
        mRichBubbleFullKeyModeSwitch.setChecked(mSharedPreferences.getBoolean(BizportContentUtil.FULLKEY_MODE, true));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "onActivityResult: resultCode=" + resultCode + " requestCode=" + requestCode);
        switch (requestCode) {
        case KEY_OPEN_FILE:
            if (resultCode == RESULT_OK) {
                Toast.makeText(SettingActivity.this, "正在导入样例，部分功能暂时无法正常使用", Toast.LENGTH_LONG).show();
                Uri uri = data.getData();
                String path = getPath(SettingActivity.this, uri);
                mLoadTaskManager.executePath(path);
            }
            break;

        default:
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void notifyImportFinish() {
        Log.i(TAG, "notifyImportFinish: ");
        new InsertSmsAlertDlg(mContext).show();
    }

    public static String getPath(Context context, Uri uri) {

        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    private void showSettingSDKDialog() {
//        if (mSettingSDKBean == null) {
//            mSettingSDKBean = new SettingSDKBean();
//            mSettingSDKBean.setOnLineUpdata(mSharedPreferences.getString(BizportContentUtil.ONLINE_UPDATE_SDK, mSettingSDKBean.ONLINE_UPDARA_SDK_YES));
//            mSettingSDKBean.setSupportNet(mSharedPreferences.getString(BizportContentUtil.SMARTSMS_ENCHANCE, Boolean.TRUE.toString()));
//            mSettingSDKBean.setSupportNetType(mSharedPreferences.getString(BizportContentUtil.SUPPORT_NETWORK_TYPE, mSettingSDKBean.SUPPORT_NETWORK_TYPE_ALL));
//            mSettingSDKBean.setCmccType(mSharedPreferences.getInt(BizportContentUtil.TOP_SWITCH_CARRIER, AbsFacadeDoAction.SWITCH_NA));
//            mSettingSDKBean.setPublicServerUrl(mSharedPreferences.getString(BizportContentUtil.CUSTOM_PUBINFO_SERVER_URL, ""));
//            mSettingSDKBean.setLogoServerUrl(mSharedPreferences.getString(BizportContentUtil.CUSTOM_LOGO_DOWN_URL, ""));
//            mSettingSDKBean.setSupportAidl(mSharedPreferences.getBoolean(BizportContentUtil.SUPPORT_AIDL, false));
//        }

//        final SettingSDKDialog dialog = new SettingSDKDialog(mContext, mSettingSDKBean);
//        dialog.show();
//        dialog.setClicklistener(new SettingSDKDialog.ClickListenerInterface() {
//            @Override
//            public void doConfirm() {
//                mSettingSDKBean = dialog.getSettingSDKBean();
//                mEditor.putString(BizportContentUtil.ONLINE_UPDATE_SDK, mSettingSDKBean.getOnLineUpdata());
//                mEditor.putString(BizportContentUtil.SMARTSMS_ENCHANCE, mSettingSDKBean.getSupportNet());
//                mEditor.putString(BizportContentUtil.SUPPORT_NETWORK_TYPE, mSettingSDKBean.getSupportNetType());
//                mEditor.putInt(BizportContentUtil.TOP_SWITCH_CARRIER, mSettingSDKBean.getCmccType());
//                mEditor.putString(BizportContentUtil.CUSTOM_PUBINFO_SERVER_URL, mSettingSDKBean.getPublicServerUrl());
//                mEditor.putString(BizportContentUtil.CUSTOM_LOGO_DOWN_URL, mSettingSDKBean.getLogoServerUrl());
//                mEditor.putBoolean(BizportContentUtil.SUPPORT_AIDL, mSettingSDKBean.getSupportAidl());
//                mEditor.commit();
//                dialog.dismiss();
//
//                //TODO
//                Process.killProcess(Process.myPid());
//            }
//
//            @Override
//            public void doCancel() {
//                dialog.dismiss();
//            }
//        });
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        deInitSmsLoader();
        super.onDestroy();
    }
}
